<?php
include 'config.php';

$coll = 'itsprop';

$class_dir = 'lib';
if (!file_exists($class_dir)) {
	mkdir ($class_dir,0755);
}
if (!file_exists($class_dir . '/Autogen')) {
	mkdir ($class_dir . '/Autogen', 0755);
}

function capFirst (&$item,$key) {
	$item = ucfirst($item);
}

$c = Dase_DBO_Collection::get($coll);

foreach ($c->getItemTypes() as $type) {
	$cols = '';
	foreach ($type->getAttributes() as $att) {
		$cols[] = "'$att->ascii'";
	}
	$cols_list = implode(',',$cols);
	$parts = explode('_',$type->ascii_id);
	array_walk($parts,'capFirst');
	$class_root_name = implode('',$parts);
	$db_class_name = 'Autogen_' . $class_root_name;
	$db_class_text = "<?php


/*
 * DO NOT EDIT THIS FILE
 * it is auto-generated by the
 * script 'bin/class_gen.php
 * 
 */

class $db_class_name  
{
	function __construct(\$assoc = false) 
	{
		parent::__construct( '$type->ascii_id',  array($cols_list));
		if (\$assoc) {
			foreach ( \$assoc as \$key => \$value) {
				\$this->\$key = \$value;
			}
		}
	}
}";		

$db_class_filepath = $class_dir . '/Autogen/' . $class_root_name . '.php';
$fh = fopen($db_class_filepath,'w');
	if (-1 == fwrite($fh,$db_class_text)) { 
		die("no go write $db_class_filepath"); 
	}
	fclose($fh) or die("no go close");
	print "created $db_class_name\n";

	$class_filepath = $class_dir . '/' . $class_root_name . '.php';
	$class_name = $class_root_name;
	if (!file_exists($class_filepath)) {
		$class_text = "<?php

require_once 'Autogen/$class_root_name.php';

class $class_name extends $db_class_name 
{

}";		
$fh = fopen($class_filepath,'w');
if (-1 == fwrite($fh,$class_text)) { 
	die("no go write $class_filepath"); 
}
fclose($fh) or die("no go close");
print "created $class_name\n";
}
}

